package com.yunsc.common.desensitize;

import cn.hutool.core.util.DesensitizedUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.yunsc.common.annotation.JsonDesensitize;

import java.io.IOException;
import java.util.Optional;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 自定义序列化器，实现对敏感字段的脱敏
 * @date: 2024-05-08 22:37
 */
public class DesensitizeSerializer extends JsonSerializer<String> implements ContextualSerializer {

    private JsonDesensitize jsonDesensitize;

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        jsonDesensitize = beanProperty.getAnnotation(JsonDesensitize.class);
        if (jsonDesensitize != null) {
            // return new DesensitizeSerializer();
            return this;
        }
        return serializerProvider.findNullKeySerializer(beanProperty.getType(), beanProperty);
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        DesensitizeType type = jsonDesensitize.type();

        String desensitized = Optional.ofNullable(type)
                .map(t -> {
                    switch (t) {
                        case TEL:
                            // 自定义逻辑
                            // return DesensitizeType.tel(s, jsonDesensitize.symbol());
                            // hutool 实现
                            return DesensitizedUtil.mobilePhone(s);
                        case ID_CARD:
                            // 自定义逻辑
                            // return DesensitizeType.idCard(s, jsonDesensitize.symbol(), jsonDesensitize.length() == -1 ? 16 : jsonDesensitize.length());
                            // hutool 实现
                            return DesensitizedUtil.idCardNum(s, 2, 2);
                        case EMAIL:
                            // 自定义逻辑
                            // return DesensitizeType.email(s, jsonDesensitize.symbol(), jsonDesensitize.length() == -1 ? 8 : jsonDesensitize.length());
                            // hutool 实现
                            return DesensitizedUtil.email(s);
                        case PASSWORD:
                            // 自定义逻辑
                            // return DesensitizeType.password(s, jsonDesensitize.symbol(), jsonDesensitize.length() == -1 ? s.length() : jsonDesensitize.length());
                            // hutool 实现
                            return DesensitizedUtil.password(s);
                        case CUSTOM:
                            // 自定义逻辑
                            return DesensitizeType.custom(s, jsonDesensitize.symbol(), jsonDesensitize.length(), jsonDesensitize.regex(), jsonDesensitize.group());
                        default:
                            return s;
                    }
                })
                .orElse(s);
        jsonGenerator.writeString(desensitized);
    }

}
